#!/bin/bash

echo "EbbFlow vs Turritopsis Clean Performance Comparison"
echo "=================================================="

# Clean logs
rm -rf logs/*
mkdir -p logs

# Configuration
NODES=4
EPOCHS=10
R=3
BATCH_SIZE=1000
TEST_DURATION=30

echo "Test Configuration:"
echo "  - Nodes: $NODES"
echo "  - Epochs: $EPOCHS"
echo "  - ADKR Interval (R): $R"
echo "  - Batch Size: $BATCH_SIZE"
echo "  - Duration: ${TEST_DURATION} seconds"
echo ""

# === Test EbbFlow ===
echo "=== Testing EbbFlow Protocol ==="
cd /home/ebbflow

# Start EbbFlow servers
for i in $(seq 0 $((NODES-1))); do
    ./bin/ebbflow_server -id $i -n $NODES -f 1 > logs/ebbflow_server_${i}.log 2>&1 &
    echo "Started EbbFlow server $i"
    sleep 0.1
done

sleep 3

echo "Starting EbbFlow client..."
./bin/ebbflow_client -id 100 -batch $BATCH_SIZE -count 10000 -txsize 250 > logs/ebbflow_client.log 2>&1 &
EBBFLOW_CLIENT_PID=$!

echo "Running EbbFlow test for ${TEST_DURATION} seconds..."
sleep $TEST_DURATION

echo "Stopping EbbFlow processes..."
killall ebbflow_server > /dev/null 2>&1
kill $EBBFLOW_CLIENT_PID > /dev/null 2>&1
sleep 2

echo ""

# === Test Turritopsis ===
echo "=== Testing Turritopsis Protocol ==="
cd /home/Turritopsis

echo "Starting Turritopsis test..."
./scripts/run_local_turritopsis.sh $NODES $EPOCHS $TEST_DURATION > logs/turritopsis_test.log 2>&1

echo ""

# === Performance Analysis ===
echo "=== Clean Performance Analysis ==="
echo ""

echo "EbbFlow Performance:"
echo "-------------------"
if [ -f "/home/ebbflow/logs/ebbflow_client.log" ]; then
    SUBMITTED=$(grep "Submitted:" /home/ebbflow/logs/ebbflow_client.log | awk '{print $2}')
    ELAPSED=$(grep "Submitted:" /home/ebbflow/logs/ebbflow_client.log | awk '{print $5}')
    SUBMISSION_TPS=$(grep "Submission TPS:" /home/ebbflow/logs/ebbflow_client.log | awk '{print $4}')
    
    echo "  - Transactions Submitted: ${SUBMITTED:-0}"
    echo "  - Submission Time: ${ELAPSED:-0} seconds"
    echo "  - Submission TPS: ${SUBMISSION_TPS:-0}"
    
    # Count epoch activities
    EPOCH_COUNT=$(grep -c "epoch" /home/ebbflow/logs/ebbflow_server_0.log)
    ADKR_COUNT=$(grep -c "ADKR" /home/ebbflow/logs/ebbflow_server_0.log)
    ACS_COUNT=$(grep -c "ACS" /home/ebbflow/logs/ebbflow_server_0.log)
    
    echo "  - Epoch Activities: ${EPOCH_COUNT:-0}"
    echo "  - ADKR Executions: ${ADKR_COUNT:-0}"
    echo "  - ACS Executions: ${ACS_COUNT:-0}"
else
    echo "  - EbbFlow client log not found"
fi

echo ""
echo "Turritopsis Performance:"
echo "----------------------"
LATEST_DIR=$(ls -t /home/Turritopsis/data/ | head -1)
if [ -n "$LATEST_DIR" ] && [ -f "/home/Turritopsis/data/$LATEST_DIR/logs/node-0.log" ]; then
    NODE_LOG="/home/Turritopsis/data/$LATEST_DIR/logs/node-0.log"
    
    # Extract Turritopsis metrics
    TRANSACTIONS=$(grep -o "decided [0-9]* items" "$NODE_LOG" | awk '{sum += $2} END {print sum+0}')
    DURATION=$(grep "completed.*epochs in" "$NODE_LOG" | awk '{print $NF}' | sed 's/s//')
    
    echo "  - Transactions Processed: ${TRANSACTIONS:-0}"
    echo "  - Duration: ${DURATION:-0} seconds"
    
    if [ "${TRANSACTIONS:-0}" -gt 0 ] && [ "${DURATION:-0}" != "0" ]; then
        TPS=$(echo "scale=2; ${TRANSACTIONS} / ${DURATION}" | bc 2>/dev/null || echo "0")
        echo "  - TPS: ${TPS:-0}"
    fi
    
    # Extract ADKR metrics
    ADKR_EXECUTIONS=$(grep -c "ADKR completed" "$NODE_LOG")
    AVG_ADKR_TIME=$(grep "ADKR completed in" "$NODE_LOG" | awk '{print $NF}' | sed 's/ms//' | awk '{sum += $1; count++} END {if(count>0) print sum/count; else print 0}')
    
    echo "  - ADKR Executions: ${ADKR_EXECUTIONS:-0}"
    echo "  - Average ADKR Time: ${AVG_ADKR_TIME:-0}ms"
    
    # Count epoch activities
    EPOCH_COUNT=$(grep -c "epoch" "$NODE_LOG")
    ACS_COUNT=$(grep -c "ACS decided" "$NODE_LOG")
    
    echo "  - Epoch Activities: ${EPOCH_COUNT:-0}"
    echo "  - ACS Executions: ${ACS_COUNT:-0}"
else
    echo "  - Turritopsis result not found"
fi

echo ""
echo "=== Clean Comparison Summary ==="
echo ""

# Extract key metrics for comparison
EBBFLOW_TPS=$(grep "Submission TPS:" /home/ebbflow/logs/ebbflow_client.log | awk '{print $4}' | head -1)
TURRITOPSIS_TPS=$(echo "scale=2; ${TRANSACTIONS:-0} / ${DURATION:-1}" | bc 2>/dev/null || echo "0")

echo "Protocol Performance Comparison:"
echo "  - EbbFlow TPS: ${EBBFLOW_TPS:-0}"
echo "  - Turritopsis TPS: ${TURRITOPSIS_TPS:-0}"

if [ "${EBBFLOW_TPS:-0}" != "0" ] && [ "${TURRITOPSIS_TPS:-0}" != "0" ]; then
    RATIO=$(echo "scale=2; ${EBBFLOW_TPS} / ${TURRITOPSIS_TPS}" | bc 2>/dev/null || echo "0")
    echo "  - EbbFlow/Turritopsis Ratio: ${RATIO}"
    
    if (( $(echo "$RATIO > 1" | bc -l) )); then
        echo "  - EbbFlow is ${RATIO}x faster than Turritopsis"
    else
        echo "  - Turritopsis is $(echo "scale=2; 1/$RATIO" | bc -l)x faster than EbbFlow"
    fi
fi

echo ""
echo "Architecture Clarification:"
echo "=========================="
echo "EbbFlow Components:"
echo "  - WaterBear: Core BFT consensus (cannot be replaced with sDumbo)"
echo "  - ADKR: Asynchronous Dynamic Key Refresh"
echo "  - ACS: Atomic Consensus (can use sDumbo internally)"
echo ""
echo "Turritopsis Components:"
echo "  - sDumbo ACS: Atomic Consensus"
echo "  - ADKR: Asynchronous Dynamic Key Refresh"
echo "  - MVBA: Multi-Valued Byzantine Agreement"

echo ""
echo "Clean performance comparison completed."


